package com.test.langchain4j.controller;

import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.output.TokenUsage;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 *
 * @description:
 * @author: liuziyang
 * @since: 2025/7/30 16:13
 * @modifiedBy:
 * @version: 1.0
 */
@RestController
@Slf4j
public class LowApiController {
  @Resource(name = "qwen")
  private ChatModel chatModelQwen;

  @Resource(name = "deepseek")
  private ChatModel chatModelDeepseek;

  /**
   * Api 01 string.
   *
   * @param prompt the prompt
   * @return the string
   */
  @GetMapping("/lowapi/api01")
  public String api01(@RequestParam(value = "prompt", defaultValue = "你是谁") String prompt) {
    String result = chatModelQwen.chat(prompt);
    log.info("Qwen问答结果: {}", result);
    return result;
  }

  /**
   * Api 02 string.
   *
   * @param prompt the prompt
   * @return the string
   */
  @GetMapping("/lowapi/api02")
  public String api02(@RequestParam(value = "prompt", defaultValue = "你是谁") String prompt) {
    ChatResponse chatResponse = chatModelDeepseek.chat(UserMessage.from(prompt));
    String result = chatResponse.aiMessage().text();
    log.info("大模型问答结果: {}", result);
    // 获取token使用情况
    TokenUsage tokenUsage = chatResponse.tokenUsage();
    log.info("Token使用情况: {}", tokenUsage);
    return result;
  }
}
